package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.unionpay.advisors.UnionpayAdvisorsData;
import com.ucredit.babel.common.model.unionpay.advisors.UnionpayAdvisorsFtRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 银联智策交易失败类变量
 *
 * @author renzhanzheng
 */

public class UnionpayAdvisorsFtParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     *
     * 银联智策交易失败类变量
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        UnionpayAdvisorsFtRecord obj = (UnionpayAdvisorsFtRecord)model;
        UnionpayAdvisorsData data = obj.getData()  == null ? new UnionpayAdvisorsData() : obj.getData();
        JSONArray objects = data.getData() == null ? new JSONArray() : JSON.parseArray(data.getData());

        StringBuilder sb = getBaseInfo(obj, successful);
        //应答码
        sb.append(data.getErrorCode() == null ? "" : data.getErrorCode()).append(CRT001);
        //请求码
        sb.append(data.getRequestCode() == null ? "" : data.getRequestCode()).append(CRT001);
        //响应码
        sb.append(data.getResponseCode() == null ? "" : data.getResponseCode()).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        StringBuilder detailSb = new StringBuilder(500);
        int j = 0;
        for(int i = 0 ;i < objects.size();i++){
            j++;
            JSONObject o = objects.getJSONObject(i);
            //序号
            detailSb.append(i+1).append(CRT001);
            //近十二个月资金不足天数
            detailSb.append(o.getString("TSJY054") == null ? "" : o.getString("TSJY054")).append(CRT001);
            //近三个月资金不足天数
            detailSb.append(o.getString("TSJY046") == null ? "" : o.getString("TSJY046")).append(CRT001);
            //近一个月资金不足天数
            detailSb.append(o.getString("TSJY042") == null ? "" : o.getString("TSJY042")).append(CRT001);
            //近一个月资金不足笔数
            detailSb.append(o.getString("TSJY040") == null ? "" : o.getString("TSJY040")).append(CRT001);
            //近三个月所有失败交易天数
            detailSb.append(o.getString("TSJY007") == null ? "" : o.getString("TSJY007")).append(CRT001);
            //近一个月所有失败交易天数
            detailSb.append(o.getString("TSJY004") == null ? "" : o.getString("TSJY004")).append(CRT001);
            //近一个月所有失败交易笔数
            detailSb.append(o.getString("TSJY002") == null ? "" : o.getString("TSJY002")).append(CRT001);
            //近十二个月不参与清算账户服务笔数
            detailSb.append(o.getString("TSJY039") == null ? "" : o.getString("TSJY039")).append(CRT001);
            //近六个月不参与清算账户服务笔数
            detailSb.append(o.getString("TSJY038") == null ? "" : o.getString("TSJY038")).append(CRT001);
            //近三个月不参与清算账户服务笔数
            detailSb.append(o.getString("TSJY037") == null ? "" : o.getString("TSJY037")).append(CRT001);
            //近十二个月发生资金不足交易商户数
            detailSb.append(o.getString("TSJY055") == null ? "" : o.getString("TSJY055")).append(CRT001);
            //近十二个月最近一笔失败交易日期
            detailSb.append(o.getString("TSJY013") == null ? "" : o.getString("TSJY013")).append(CRT001);
            //近一个月所有失败交易金额
            detailSb.append(o.getString("TSJY003") == null ? "" : o.getString("TSJY003")).append(CRT001);
            //近六个月资金不足天数
            detailSb.append(o.getString("TSJY050") == null ? "" : o.getString("TSJY050")).append(CRT001);
            //近一个月资金不足金额
            detailSb.append(o.getString("TSJY041") == null ? "" : o.getString("TSJY041")).append(CRT001);

            key.set(getKey());
            val.set((sb.toString() + detailSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        if (j == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_yinlianzhice_jiaoyishibai_inc";
    }

    @Override
    public String getType() {
        return RecordType.UNIONPAY_ADVISORS_FT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "银联智策交易失败类变量";
    }
}
